﻿using PMS.Domain.Exceptions;
using PMS.Domain.Seedwork;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PMS.Domain.AggregatesModel.ProjectAggregate
{
    public class ProjectStatus:Enumeration
    {

        public static ProjectStatus Create = new ProjectStatus(1, "创建");
        public static ProjectStatus Audit =  new ProjectStatus(2, "审核");
        public static ProjectStatus Excute = new ProjectStatus(3, "实施");
        public static ProjectStatus Finish = new ProjectStatus(4, "完成");

        protected ProjectStatus()
        {
        }

        public ProjectStatus(int id, string name)
            : base(id, name)
        {
        }

        public static IEnumerable<ProjectStatus> List() =>
            new[] { Create, Audit, Finish };

        /// <summary>
        /// 根据名称获取 审核类型
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static ProjectStatus FromName(string name)
        {
            var state = List()
                .SingleOrDefault(s => String.Equals(s.Name, name, StringComparison.CurrentCultureIgnoreCase));

            if (state == null)
            {
                throw new PMSDomainException($"数据不存在，可用项目状态有: {String.Join(",", List().Select(s => s.Name))}");
            }

            return state;
        }

        /// <summary>
        /// 根据ID获取项目状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static ProjectStatus From(int id)
        {
            var state = List().SingleOrDefault(s => s.Id == id);

            if (state == null)
            {
                throw new PMSDomainException($"数据不存在，可用项目状态有: {String.Join(",", List().Select(s => s.Name))}");
            }

            return state;
        }

    }
}
